
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import crm.salesOrderProcessing.entity.SalesInquiryEntity;
import crm.salesOrderProcessing.session.SalesInquirySessionBean;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;


@ManagedBean
@ViewScoped
public class DeleteSalesInquiryManagedBean implements Serializable{
  
    public Long id;  
    @EJB
    SalesInquirySessionBean salesInquirySessionBean;
    @EJB
    EmployeeSessionBean employee;
    private SalesInquiryEntity selectedSalesInquiry;
  

    public DeleteSalesInquiryManagedBean() {
        selectedSalesInquiry = new SalesInquiryEntity();
    }
    
    
    public void viewInquiry(ActionEvent event) throws IOException{
        SalesInquiryEntity si=(SalesInquiryEntity)event.getComponent().getAttributes().get("viewInquiry");
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("newInquiry", selectedSalesInquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("viewSalesInquiry.xhtml");
    }
    

    public List<SalesInquiryEntity> getSalesInquiry() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        Long loginId = (Long) request.getSession().getAttribute("userId");
        SalesmanEntity user = employee.getSalesmanById(loginId);
        
        if (user.getSalesManLevel().equals("4")){
            return salesInquirySessionBean.getAllSalesInquiry();
        }
        else{
            return salesInquirySessionBean.getInquiryByUser(user);
        }
    }

    public void deleteSalesInquiry (ActionEvent event) {
       id=(Long)event.getComponent().getAttributes().get("deleteInquiry");
       salesInquirySessionBean.deleteSalesInquiry(id);
       selectedSalesInquiry = new SalesInquiryEntity();
    }

    public SalesInquiryEntity getSelectedSalesInquiry() {
        return selectedSalesInquiry;
    }

    public void setSelectedSalesInquiry(SalesInquiryEntity selectedSalesInquiry) {
        this.selectedSalesInquiry = selectedSalesInquiry;
    }
       
}
